//
//  ViewController.m
//  Test1
//
//  Created by clip on 16/3/28.
//  Copyright © 2016年 clip. All rights reserved.
//

#import "ViewController.h"
#import <sqlite3.h>
#import "RecommendCellView.h"

#define kDataBaseFilePath [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.sqlite"]

@interface ViewController ()
@property (nonatomic, strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(100, 100, 100, 50);
    _button.backgroundColor = [UIColor redColor];
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
    
    [self initDataBase];
    
    
    RecommendCellView *view = [[RecommendCellView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    
    
    [self.view addSubview:view];
}

- (void)buttonAction:(UIButton *)button {
    
    UIAlertController *ale = [UIAlertController alertControllerWithTitle:@"警告" message:@"密码错误" preferredStyle:UIAlertControllerStyleAlert];
    
    
      [ale addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [ale addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    
   
    [ale addAction:[UIAlertAction actionWithTitle:@"clip" style:UIAlertActionStyleDestructive handler:nil]];
    [self presentViewController:ale animated:YES completion:nil];
}

- (void)initDataBase {
    
    NSLog(@"%@",NSHomeDirectory());
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:kDataBaseFilePath]) {
        
        NSLog(@"文件已存在");
        
        return;
    }
    
    [fileManager createFileAtPath:kDataBaseFilePath contents:nil attributes:nil];
    
    sqlite3 *sqlite3 = NULL;
    
   int result = sqlite3_open([kDataBaseFilePath UTF8String], &sqlite3);
    
    if (result != SQLITE_OK) {
        
        
        NSLog(@"数据库打开失败");
        
        return;
    }
    
    NSString *sqlString = @"CREATE TABLE User(name text NOT NULL,userID text PRIMARY KEY,password text NOT NULL)";
    result = sqlite3_exec(sqlite3, [sqlString UTF8String], NULL, NULL, NULL);

    if (result != SQLITE_OK) {
        
        NSLog(@"创建表格失败");
        
        sqlite3_close(sqlite3);
        
        return;
        
    }
    
    NSLog(@"数据库创建成功");
    
    sqlite3_close(sqlite3);
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
